package com.yhsimon.controller;


import com.yhsimon.entity.AdviceVO;
import com.yhsimon.entity.User;
import com.yhsimon.entity.UserVO;
import com.yhsimon.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")
public class UserHandler {

    @Autowired
    private UserFeign userFeign;

    @GetMapping("/findAll")
    @ResponseBody
    public UserVO findAll(@RequestParam("page") int page, @RequestParam("limit") int limit) {
        return userFeign.findAll(page, limit);
    }

    @GetMapping("/findById/{id}")
    @ResponseBody
    public User findById(@PathVariable("id") int id) {
        return userFeign.findById(id);
    }

    @GetMapping("/findById2/{id}")
    public String findById(@PathVariable("id") int id, Model model) {
        model.addAttribute("user", userFeign.findById(id));
        return "user_update";
    }

    @GetMapping("/redirect/{location}")
    public String redirect(@PathVariable("location") String location) {
        return location;
    }

    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") int id) {
        userFeign.deleteBytId(id);
        return "redirect:/user/redirect/user_manage";
    }

    @GetMapping("/deleteAdviceById/{id}")
    public String deleteAdviceById(@PathVariable("id") int id) {
        userFeign.deleteAdviceById(id);

        return "user_all_advice";
    }



    @PostMapping("/update")
    public String update(User user,Model model) {
        userFeign.update(user);
        model.addAttribute("uMsg", "账户信息修改成功！");
        return "user_update";
    }


    @PostMapping(value = "/save")
    public String save(User user,Model model) {
        User usr = userFeign.findByName(user.getUserName());
        if(usr!=null){
            model.addAttribute("rMsg","error" );
            return "user_add";
        }else{
            model.addAttribute("rMSg", "success");
            userFeign.save(user);
            return "user_manage";
        }


    }

    @PostMapping(value = "/register")
    public String register(User user,Model model) {
        User usr = userFeign.findByName(user.getUserName());
        if(usr!=null){
            model.addAttribute("rMsg","error" );
            return "register";
        }else{
            model.addAttribute("rMSg", "success");
            userFeign.save(user);
            return "login";
        }

    }


    @PostMapping("/advice")
    public String advice(@RequestParam("advice") String advice,@RequestParam("id") int id, Model model){
        model.addAttribute("adviceMsg", "感谢参与！");
        System.out.println("client:  id=="+id+"advice");
        userFeign.advice(id,advice);

        return "user_advice";

    }

    @GetMapping("/findAllAdvice")
    @ResponseBody
    public AdviceVO findAllAdvice(@RequestParam("page") int page, @RequestParam("limit") int limit){
        return userFeign.findAllAdvice(page,limit);
    }
}
